#!/bin/bash
node_list="$1"
source_client="./client_module/"

if [ ! -d ${source_client} ];then
	echo "${source_client} is not exist"
    echo "Usage: sh $0 96.232"
	exit 1
fi

pw_str="p@ssword"
fs_version="4.0.8"
remote_client="/opt/orcafs/src/client/client_module_${fs_version}"
ko_path1="/lib/modules/4.18.0-240.el8.x86_64/updates/fs/orcafs_autobuild"
ko_path2="/lib/modules/4.18.0-240.el8.x86_64/updates/fs/orcafs"

function update_client()
{
    # update orcafs-api for all nodes
    current=$(date "+%Y%m%d%H%M%S")
    for node in "${node_list[@]}"; do
        echo -------------"${node}"-----------
        sshpass -p $pw_str rsync -av --delete ${source_client} root@192.168."${node}":${remote_client}
        sshpass -p $pw_str ssh root@192.168."${node}" <<EOF &
systemctl stop orcafs-client
umount /mnt/orcafs
umount /mnt/default
rmmod orcafs
#make -j20 -C ${remote_client}/build/ ORCAFS_VERSION=4.0.6-fix OFED_INCLUDE_PATH=/usr/src/ofa_kernel/default/include
make -j20 -C ${remote_client}/build/ ORCAFS_VERSION=$current OFED_INCLUDE_PATH=/usr/src/ofa_kernel/default/include
#ls -l  ${remote_client}/build/
#mv ${remote_client}/build/orcafs.ko ${remote_client}/build/bk_orcafs.ko 
#mv ${remote_client}/build/orcafs-unstripped.ko ${remote_client}/build/orcafs.ko
insmod ${remote_client}/build/orcafs-unstripped.ko
mkdir -p ${ko_path1}
mkdir -p ${ko_path2}
/usr/bin/cp -f ${remote_client}/build/orcafs-unstripped.ko ${ko_path1}/orcafs.ko
/usr/bin/cp -f ${remote_client}/build/orcafs-unstripped.ko ${ko_path2}/orcafs.ko
systemctl start orcafs-client
sleep 1

#version=\$(modinfo orcafs | grep -w version | awk '{print \$2}')
version=\$(grep "Version" /var/log/orcafs-client.log | tail -1 | awk '{print \$10}')
echo "ver=\${version}, current=${current}"
if [ "${current}" == "\${version}" ]; then
    echo "Update SUCCESS"
else
    echo "Update FAILED"
fi

EOF
    done
    wait
}

update_client
